#!/bin/bash

idandheads=$(grep "cyclehead" $1 | cut -d "," -f 1 | cut -d ":" -f 1,3 | sort | uniq)

#echo idandheads: $idandheads
rm -f $1.sqlin

for ih in $idandheads; do
   i=$(echo $ih | cut -d ":" -f 1)
   echo -n 0:$ih: >>$1.sqlin
  grep "^$i" $1 | grep "cyclehead" | cut -d ":" -f 3- | tr "," "\n" | sort | uniq | tr "\n" "," | sed -e "s/,$/\n/g" >>$1.sqlin
  grep "^$i" $1  | grep "EXTERNAL\|INTERNAL" | cut -d ":" -f 2- | tr "," "\n" | sort | uniq | tr "\n" "," | sed -e "s/,$/\n/g" | sed -e "s/^/0:/g" >>$1.sqlin
done
